﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Data.UserDb;

namespace RoomWeb
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class queryInfo : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "GB2312";
            context.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            String action = context.Request.Params["action"];
            if (String.IsNullOrEmpty(action)) return;

            switch (action.ToLower())
            {
                case "topnnews"://获取广场动态
                    GetTopNNews(10, context);
                    break;
                case "getroomad":// 获取经典欣赏
                    GetRoomAd(context);
                    break;
                case "getroomlist":
                    GetRoomList(10, context);
                    break;
                case "gettv":
                    GetProg(1, 10, context);
                    break;
                case "getmovie":
                    GetProg(0, 10, context);
                    break;
                case "getexp":
                    GetUser(0, 10, context);
                    break;
                case "getmoney":
                    GetUser(1, 10, context);
                    break;
                    
            }
        }

        private static void GetUser(int ordertype, int topn, HttpContext context)
        {

            String tmp = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"; //"<dl style=\"width:100%\">";
            var i = 1;
            Array.ForEach(Media.GetUserOrser(ordertype, topn).ToArray(), s =>
            {
                tmp +=
                    String.Format(
                        "<tr><td width=\"20\"><img src=\"images/ico{2}.gif\" /></td><td width=\"105\" class=\"chaochu\">{0}</td><td width=\"30\">{1}</td></tr>",
                        s.Username, ordertype == 0 ? s.Exp : s.Money, i++);
            });
            tmp += "</table>";
            context.Response.Write(tmp);
        }

        private static void GetProg(int type, int topn, HttpContext context)
        {
            String tmp = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"; //"<dl style=\"width:100%\">";
            var i = 1;
            Array.ForEach(Media.GetTopProg(type, topn).ToArray(), s =>
            {
                tmp +=
                    String.Format(
                        "<tr><td width=\"20\"><img src=\"images/ico{2}.gif\" /></td><td width=\"105\" class=\"chaochu\">{0}</td><td width=\"30\">{1}</td></tr>",
                        s.Name, s.Times, i++);
            });
            tmp += "</table>";
            context.Response.Write(tmp);
        }

        private static void GetRoomList(int topn, HttpContext context)
        {
            String tmp = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"; //"<dl style=\"width:100%\">";
            var i = 1;
            Array.ForEach(Media.GetRoomInfoByExp(topn).ToArray(), s=>
                                                                      {
                                                                          tmp +=
                                                                              String.Format(
                                                                                  "<tr><td width=\"20\"><img src=\"images/ico{2}.gif\" /></td><td width=\"105\" class=\"chaochu\"><a href=\"roominfo.aspx?roomid={4}\" >{0}</a></td><td width=\"30\">{1}</td></tr>",
                                                                                  s.Roomname, s.Exp, i++, s.Chnlid, s.Roomid, s.Roomipaddr, s.Roomipaddr.Length);
                                                                      });
            tmp += "</table>";
            context.Response.Write(tmp);
        }

        /// <summary>
        /// 获取经典欣赏
        /// </summary>
        /// <param name="context"></param>
        private static void GetRoomAd(HttpContext context)
        {
            String t = "<marquee direction=\"left\" onmouseover=\"this.stop()\" onmouseout=\"this.start()\" ><table cellSpacing=0 cellPadding=0 align=left border=0  id=\"_mlist\"><tr>";
            Array.ForEach(Media.GetRoomAd().ToArray(), ad=>
                                                           {
                                                               var template =
                                                                   string.Format(
                                                                       "<td width=\"140\" class=\"lst\"><a href=\"{0}\"><img src=\"{1}\" class=\"ad_img\" /></a></td>",
                                                                       ad.Url, ad.Image);
                                                               t += template;
                                                               
                                                           });
            t += "</tr></table></marquee>";
            context.Response.Write(t);
        }

        /// <summary>
        /// 获取广场动态
        /// </summary>
        /// <param name="p">获取条数</param>
        /// <param name="context"></param>
        private static void GetTopNNews(int p, HttpContext context)
        {
            Array.ForEach(
                Media.GetTopnNews(p).ToArray(),
                news=>{
                    var template = String.Format("<span><font color='#c6000d'>{0}</font>{1}</span><br/>", ((DateTime)news.Createtime).ToString("yyyy年MM月dd日hh时mm分ss秒"), news.Msg);
                              context.Response.Write(template);
                          });
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
